import os
import requests

# 确保 static 目录存在
def ensure_dir(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)

# 下载文件
def download_file(url, save_path):
    print(f"正在下载: {url}")
    response = requests.get(url)
    response.raise_for_status()  # 如果请求失败，抛出异常
    with open(save_path, 'wb') as f:
        f.write(response.content)
    print(f"已保存: {save_path}")

# 主函数
def main():
    # 确保 static/css、static/js 和 static/webfonts 目录存在
    ensure_dir('static/css')
    ensure_dir('static/js')
    ensure_dir('static/webfonts')
    
    # 定义要下载的文件列表
    files_to_download = [
        # CSS 文件
        ('https://cdnjs.cloudflare.com/ajax/libs/admin-lte/3.2.0/css/adminlte.min.css', 'static/css/adminlte.min.css'),
        ('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css', 'static/css/all.min.css'),
        
        # JS 文件
        ('https://cdn.jsdelivr.net/npm/chart.js', 'static/js/chart.min.js'),
        ('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js', 'static/js/jquery.min.js'),
        ('https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.2/js/bootstrap.bundle.min.js', 'static/js/bootstrap.bundle.min.js'),
        ('https://cdnjs.cloudflare.com/ajax/libs/admin-lte/3.2.0/js/adminlte.min.js', 'static/js/adminlte.min.js'),
        
        # Font Awesome 字体文件
        ('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/webfonts/fa-solid-900.woff2', 'static/webfonts/fa-solid-900.woff2'),
        ('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/webfonts/fa-regular-400.woff2', 'static/webfonts/fa-regular-400.woff2'),
        ('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/webfonts/fa-solid-900.ttf', 'static/webfonts/fa-solid-900.ttf'),
        ('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/webfonts/fa-regular-400.ttf', 'static/webfonts/fa-regular-400.ttf'),
    ]
    
    # 下载所有文件
    for url, save_path in files_to_download:
        try:
            download_file(url, save_path)
        except Exception as e:
            print(f"下载失败: {url}, 错误: {str(e)}")
    
    print("所有文件下载完成！")

if __name__ == '__main__':
    main()